import * as NSBase from '../NetServiceBase';
import { IMemberManager } from '../IManager';
import { IsNull } from '../../TYLibrary/Common/GlobalExtend';
import { Logger, LogLevel, T_USER } from '../EntityBase';
import { ManagerBase } from './ManagerBase';

//V_LoginUser
//NS.UserLogin

export class MemberManager extends ManagerBase implements IMemberManager {
	public LoginUser: T_USER = new T_USER();
	public IsShowReceive: boolean = false;

	public UserLoginUrl: string;

	private async _UserLogin() {
		try {
			Logger.Debug(LogLevel.L1, 'UserInit-LaunchData...', this.m_App.LaunchData);
			if (this.m_App.LaunchData.shareTicket && this.m_App.LaunchData.query.ReferralCode) {
				let s = await this.m_App.Wx_GetShareInfo(this.m_App.LaunchData.shareTicket, 10000);
				if (!IsNull(s)) {
					this.LoginUser = await NSBase.UserLogin(
						this.UserLoginUrl,
						this.m_App.WXLoginInfo.Name,
						this.m_App.WXLoginInfo.PICUrl,
						this.m_App.WXLoginInfo.UnionID,
						this.m_App.WXLoginInfo.OpenID,
						this.m_App.LaunchData.query.ReferralCode,
						s.encryptedData,
						s.iv
					);
				}
			} else {
				this.LoginUser = await NSBase.UserLogin(
					this.UserLoginUrl,
					this.m_App.WXLoginInfo.Name,
					this.m_App.WXLoginInfo.PICUrl,
					this.m_App.WXLoginInfo.UnionID,
					this.m_App.WXLoginInfo.OpenID,
					this.m_App.LaunchData.query.ReferralCode || undefined
				);
			}
		} catch (error) {
			Logger.Error(LogLevel.L1, error);
		}
	}

	public async Init() {
		if (!IsNull(this.m_App.WXLoginInfo) && !IsNull(this.m_App.WXLoginInfo.UnionID)) {
			await this._UserLogin();
		}
	}

	//public async OnAuthSuccess() { }
}
